<?php
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport( 'joomla.application.component.model' );
JTable::addIncludePath(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_ecommerce'.DS.'tables');	

class EcommerceModelSearch extends JModel
{
	var $_product = NULL;

	var $_keyword = NULL;
	var $_manufacturer = NULL;

	var $_where = NULL;

	function __construct()
	{
		global $mainframe;
		parent::__construct();
		$keyword = JRequest::getVar('keyword');
		$this->_keyword = $keyword;
		$manufacturer = JRequest::getVar('manufacturer');
		$this->_manufacturer = $manufacturer;	
	}
	function getSearch()
	{
		if(!$this->_product)
		{		
			global $mainframe;
					
			$where=' ';
			if(!empty($this->_keyword)){
				$where .= " AND ((p.name LIKE '%".$this->_keyword."%') || (p.number = '".$this->_keyword."'))"; 
			}
			
			if(!empty($this->_manufacturer)){
				$where .= " AND com.name LIKE '%".$this->_manufacturer."%'";
			}
			
			$query = "SELECT p.*, com.name AS com_name FROM #__pr_product p 
				LEFT JOIN #__pr_product_category pc on p.id = pc.product_id 
				LEFT JOIN #__pr_category c on pc.category_id = c.id 
				LEFT JOIN #__pr_company com on com.id = p.manufacturer 
				WHERE p.published = 1 " . $where ;
			$this->_db->setQuery($query);	
			$this->_product = $this->_db->loadObjectList();
			
			return $this->_product;
		}
	}
}
?>